package com.wmlive.hhvideo.heihei.personal.presenter;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.login.MobileLoginResponse;
import com.wmlive.hhvideo.heihei.beans.personal.UpdateUserResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.heihei.login.AccountUtil;
import com.wmlive.hhvideo.heihei.personal.view.IPersonalInfoView;
import com.wmlive.networklib.entity.DCNetObserver;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by XueFei on 2017/6/1.
 *
 * 个人信息页
 */

public class PersonalInfoPresenter extends BasePresenter<IPersonalInfoView> {
    public PersonalInfoPresenter(IPersonalInfoView view) {
        super(view);
    }

    /**
     * 更新用户
     * @param name
     * @param gender
     * @param birth_day
     * @param region
     * @param description
     * @param cover_ori
     * @param cover_ori_file_sign
     */
    public void updateUser(String name, String gender, String birth_day, String region, String description, String cover_ori, String cover_ori_file_sign) {
        String url = InitCatchData.userUpdateUser();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_UPDATE_USER, getHttpApi().updateUser(url, name, gender, birth_day, region, description, cover_ori, cover_ori_file_sign))
                    .subscribe(new DCNetObserver<UpdateUserResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, UpdateUserResponse response) {
                            if (null != viewCallback) {
                                final MobileLoginResponse mobileLoginResponse = AccountUtil.getAccount();
                                mobileLoginResponse.setUserInfo(response.getUser_info());
                                if (!TextUtils.isEmpty(mobileLoginResponse.getToken())) {
                                    AccountUtil.resetAccount(mobileLoginResponse);
                                    Observable.just(1)
                                            .subscribeOn(Schedulers.io())
                                            .map(new Function<Integer, Boolean>() {
                                                @Override
                                                public Boolean apply(Integer integer) throws Exception {
                                                    AccountUtil.updateUserInfo(mobileLoginResponse);
                                                    return true;
                                                }
                                            })
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Consumer<Boolean>() {
                                                @Override
                                                public void accept(Boolean aBoolean) throws Exception {
                                                    viewCallback.handleInfoSucceed();
                                                }
                                            }, new Consumer<Throwable>() {
                                                @Override
                                                public void accept(Throwable throwable) throws Exception {
                                                    viewCallback.handleInfoFailure("保存失败");
                                                }
                                            });
                                } else {
                                    viewCallback.handleInfoFailure("保存失败");
                                }

                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleInfoFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }
}
